<template>
    <div>
        <Row type="flex" justify="start" align="middle" class="height-100">
            <div style="display: block;text-align: center;">
                <Upload
                        ref="upload"
                        :max-size="5120"
                        :show-upload-list="false"
                        action="https://up.qiniup.com"
                        :on-format-error="handleFormatError"
                        :before-upload="handleBeforeUpload"
                        :on-progress="handleProgress"
                        :on-success="handleSuccess"
                        :on-error="handleError"
                        :on-exceeded-size="handleExceed"
                        :data="{token:upload_token}"
                >
                    <Button icon="ios-cloud-upload-outline">上传文件</Button>
                    <span class="padding-left-20">建议尺寸：{{wid}}*{{hei}}</span>
                </Upload>
            </div>
        </Row>
        <Row class="margin-top-20">
            <div class="" v-if="icon_files.length > 0">
                <template  v-for="item in icon_files">
                    <div class="demo-upload-list" v-if="item != ''">
                        <img :src="item">
                        <div class="demo-upload-list-cover">
                            <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
                            <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
                        </div>
                    </div>
                </template>
            </div>
            <Modal title="查看图片" v-model="visible" style="z-index: 1000">
                <img :src="imgName" v-if="visible" style="width: 100%">
            </Modal>
        </Row>
    </div>
</template>
<script>
    import { getQiniu,getQiniuPrivateUrl } from '@/api/main'
    export default {
        data () {
            return {
                visible: false,
                imgName: '',
                upload_token: '',
                qiniu_url: ''
            }
        },
        props: {
            icon_files: {
                type: Array,
                default: []
            },
            wid:{
                type: Number,
                default: 150
            },
            hei:{
                type: Number,
                default: 150
            },
            uploadNum: {
                type: Number,
                default:0
            },
            uploadPrivate:{
                type: Boolean,
                default: false
            }
        },
        methods: {
            handleFormatError(file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请选择图片文件。',
                })
            },
            handleBeforeUpload(file) {
                this.$Notice.warning({
                    title: '文件正在上传',
                    desc: '文件 ' + file.name + ' 正在上传。',
                })
            },
            handleProgress(event, file) {
                // this.$Notice.info({
                //     title: '文件正在上传',
                //     desc: '文件 ' + file.name + ' 正在上传。',
                // })
            },
            handleSuccess(event, file, fileList) {
                let _this = this
                if (file.response.key) {
                    var upload_url = file.response.domain+file.response.key
                    if(this.uploadPrivate){
                        let obj = {}
                        obj.url = upload_url
                        getQiniuPrivateUrl(obj).then(res => {
                            upload_url = res.url
                            _this.addUploadImg(upload_url)
                        })
                    }else{
                        this.addUploadImg(upload_url)
                    }
                }
                this.$Notice.success({
                    title: '文件上传成功',
                    desc: '文件 ' + file.name + ' 上传成功。',
                })
            },
            addUploadImg(upload_url){
                if(this.uploadNum > 0){
                    if(this.icon_files.length < this.uploadNum){
                        this.icon_files.push(upload_url)
                    }else{
                        this.$set(this.icon_files, (this.icon_files.length - 1), upload_url)
                    }
                }else{
                    this.icon_files.push(upload_url)
                }
            },
            handleError(event, file) {
                this.$Notice.error({
                    title: '文件上传成功',
                    desc: '文件 ' + file.name + ' 上传失败。',
                })
            },
            handleExceed(event, file) {
                this.$Notice.error({
                    title: '文件上传失败',
                    desc: '文件大小超出限制。',
                })
            },
            handleView(name) {
                this.imgName = name
                this.visible = true
            },
            handleRemove(file) {
                // 从 upload 实例删除数据
                this.icon_files.splice(this.icon_files.indexOf(file), 1)
            }
        },
        mounted() {
            let obj = {}
            if(this.uploadPrivate){
                obj.is_private = 1
            }else{
                obj.is_private = 0
            }
            getQiniu(obj).then(res => {
                if (res.code === 0) {
                    this.upload_token = res.token
                    this.qiniu_url = res.url
                }
            })
        }
    }
</script>
<style>
    .demo-upload-list {
        display: inline-block;
        min-width: 60px;
        min-height: 60px;
        //max-width: 200px;
        //max-height: 200px;
        text-align: center;
        line-height: 0px;
        //border: 1px solid transparent;
        //border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
        margin-right: 4px;
    }
    .demo-upload-list img {
        width: 100%;
        height: 100%;
    }
    .demo-upload-list-cover {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, 0.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover {
        display: block;
    }
    .demo-upload-list-cover i {
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
    p.prompt {
        color: #dddee1;
    }
</style>

